package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.FfFileInfo;
import com.amsp.oa.oademo.entity.FfFileRecord;
import com.amsp.oa.oademo.mapper.FfFileInfoMapper;
import com.amsp.oa.oademo.mapper.FfFileRecordMapper;
import com.amsp.oa.oademo.service.FfFileRecordService;
import com.amsp.oa.oademo.service.FfFileService;
import com.amsp.oa.oademo.vo.FfFileInfoVO;
import com.amsp.oa.oademo.vo.FfFileRecordVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FfFileServiceImpl implements FfFileService {

    @Resource
    private FfFileInfoMapper fileInfoMapper;

    @Resource
    private FfFileRecordService ffFileRecordService;

    @Override
    public IPage<FfFileInfoVO> page(Page<FfFileInfoVO> page, String fileCategory, String fileNumber, String fileName) {
        Page<FfFileInfo> infoPage = new Page<>(page.getCurrent(), page.getSize());

        LambdaQueryWrapper<FfFileInfo> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(fileCategory)) {
            wrapper.like(FfFileInfo::getFileCategory, fileCategory);
        }
        if (StringUtils.hasText(fileNumber)) {
            wrapper.like(FfFileInfo::getFileNumber, fileNumber);
        }
        if (StringUtils.hasText(fileName)) {
            wrapper.like(FfFileInfo::getFileName, fileName);
        }

        IPage<FfFileInfo> infoIPage = fileInfoMapper.selectPage(infoPage, wrapper);

        IPage<FfFileInfoVO> result = new Page<>(infoIPage.getCurrent(), infoIPage.getSize(), infoIPage.getTotal());
        List<FfFileInfoVO> records = infoIPage.getRecords().stream().map(info -> {
            FfFileInfoVO vo = new FfFileInfoVO();
            BeanUtils.copyProperties(info, vo);

            // 查询关联的记录
            LambdaQueryWrapper<FfFileRecord> recordWrapper = new LambdaQueryWrapper<>();
            recordWrapper.eq(FfFileRecord::getFileInfoId, info.getId());
            List<FfFileRecord> fileRecords = ffFileRecordService.list(recordWrapper);

            List<FfFileRecordVO> recordVOs = fileRecords.stream().map(record -> {
                FfFileRecordVO recordVO = new FfFileRecordVO();
                BeanUtils.copyProperties(record, recordVO);
                return recordVO;
            }).collect(Collectors.toList());

            vo.setRecords(recordVOs);
            return vo;
        }).collect(Collectors.toList());

        result.setRecords(records);
        return result;
    }

    @Override
    public FfFileInfoVO getById(Long id) {
        FfFileInfo info = fileInfoMapper.selectById(id);
        if (info == null) {
            return null;
        }

        FfFileInfoVO vo = new FfFileInfoVO();
        BeanUtils.copyProperties(info, vo);

        // 查询关联的记录
        LambdaQueryWrapper<FfFileRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FfFileRecord::getFileInfoId, id);
        List<FfFileRecord> records = ffFileRecordService.list(wrapper);

        List<FfFileRecordVO> recordVOs = records.stream().map(record -> {
            FfFileRecordVO recordVO = new FfFileRecordVO();
            BeanUtils.copyProperties(record, recordVO);
            return recordVO;
        }).collect(Collectors.toList());

        vo.setRecords(recordVOs);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(FfFileInfoVO fileInfoVO) {
        // 保存或更新文件信息
        FfFileInfo info = new FfFileInfo();
        BeanUtils.copyProperties(fileInfoVO, info);
        if (info.getId() != null) {
            fileInfoMapper.updateById(info);
        } else {
            fileInfoMapper.insert(info);
        }
        // 保存新记录
        if (fileInfoVO.getRecords() != null && !fileInfoVO.getRecords().isEmpty()) {
            List<FfFileRecord> records = fileInfoVO.getRecords().stream().map(recordVO -> {
                FfFileRecord record = new FfFileRecord();
                BeanUtils.copyProperties(recordVO, record);
                record.setFileInfoId(info.getId());
                return record;
            }).collect(Collectors.toList());
            ffFileRecordService.saveOrUpdateBatch(records);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeById(Long id) {
        // 删除关联记录
        LambdaQueryWrapper<FfFileRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FfFileRecord::getFileInfoId, id);
        ffFileRecordService.remove(wrapper);
        // 删除文件信息
        fileInfoMapper.deleteById(id);
    }
}